/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.domain.KingTeamVote;
import me.zhengjie.modules.system.domain.dto.KingTeamVoteQueryCriteria;
import me.zhengjie.modules.system.domain.vo.KingTeamVoteVO;
import me.zhengjie.modules.system.mapper.KingTeamVoteMapper;
import me.zhengjie.modules.system.service.KingTeamVoteService;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author greazy
* @date 2025-06-05
**/
@Service
@RequiredArgsConstructor
public class KingTeamVoteServiceImpl extends ServiceImpl<KingTeamVoteMapper, KingTeamVote> implements KingTeamVoteService {

    private final KingTeamVoteMapper kingTeamVoteMapper;

    @Override
    public PageResult<KingTeamVote> queryAll(KingTeamVoteQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(kingTeamVoteMapper.findAll(criteria, page));
    }

    @Override
    public List<KingTeamVote> queryAll(KingTeamVoteQueryCriteria criteria){
        return kingTeamVoteMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(KingTeamVote resources) {
        kingTeamVoteMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(KingTeamVote resources) {
        KingTeamVote kingTeamVote = getById(resources.getId());
        kingTeamVote.copy(resources);
        kingTeamVoteMapper.updateById(kingTeamVote);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        kingTeamVoteMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<KingTeamVote> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (KingTeamVote kingTeamVote : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" activateId",  kingTeamVote.getActivateId());
            map.put(" groupId",  kingTeamVote.getGroupId());
            map.put(" userId",  kingTeamVote.getUserId());
            map.put(" rank",  kingTeamVote.getRank());
            map.put(" bianbianVal",  kingTeamVote.getBianbianVal());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<KingTeamVoteVO> listVoteUserName(Integer activateId, Integer groupId) {
        return kingTeamVoteMapper.listVoteUserName(activateId,groupId);
    }

    @Override
    public Integer getJoinStatus(Integer activateId, Long userId) {
        LambdaQueryWrapper<KingTeamVote> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KingTeamVote::getActivateId,activateId).eq(KingTeamVote::getUserId,userId);
        KingTeamVote kingTeamVote = kingTeamVoteMapper.selectOne(wrapper);
        if (ObjectUtil.isNotEmpty(kingTeamVote)){
            return 0;
        }else{
            return 1;
        }
    }
}